﻿using Aop.Api;
using Aop.Api.Request;
using Aop.Api.Response;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WxPayAPI;

namespace WxPayJSAPI
{
    /// <summary>
    /// 支付宝手机Wap支付
    /// </summary>
    public class AliPayConfigWap
    {
        //↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

        // 合作身份者ID，签约账号，以2088开头由16位纯数字组成的字符串，查看地址：https://b.alipay.com/order/pidAndKey.htm
        public static string partner = "2088731567039362";

        //app应用ID
        public static string app_id = "2021001168603374";

        // 收款支付宝账号，以2088开头由16位纯数字组成的字符串，一般情况下收款账号就是签约账号
        public static string seller_id = partner;

        public static string alipayWang = "https://openapi.alipay.com/gateway.do";

        //商户的私钥,原始格式，RSA公私钥生成：https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.nBDxfy&treeId=58&articleId=103242&docType=1
        public static string private_key = "MIIEpAIBAAKCAQEA2kqnchaVRTLcl6DtT6TiJepcpOnode3EsROE9s17UrbognZo5DXnDS2qQ0+bw4A+ahRJLfBn/wlJmSWzI3I/9F61HgoTJzwV1FxeQBtfFi3LHWG8kAGzOZSFfEHi+I0Pj7wCd7CMAmqsWinD0kwmfNKptlbTdqa7MBRn6ppReFIsKwCJtJRNNEqO748oRy0qW9ELtN6txD/cDW+BNu55Os6sbhRoWjlaKqVD7n30TLcKbw0ihAephphrc2yC+upUp5vxUCkZzy6bpqbWpTcCjbzoFaZ+KnLw+rhncWTk/dfGjAslT6f0rq+ibMczFbOQSEsZXjazfeCmyDpOYyXK3QIDAQABAoIBAA/0YOSvzd0u6HyUoIOvs2+9/2N2TS1BYZznplI7loXnJHtUmDoFzotiCQ2+Zc4WM3mhXe/joFY9oYl1/e/bS91ahrZZWikF9Tp+Pc1Fst92HWBaYmPMHANbXZ5FWod8F+UTgsKovrL2hP5aCxv1wOJiq1DOa8pCSXAsLaQx/z5h8IusxrdrTOmPJmTYdHZuXkB7OVG/1adbhswCzNpw/j5Q0CgUjVbbGrfYE5Jydx50ApkpIfL1RN/6wSNTtHBzYkIAzR1ebZtDFMRHj7OANiOVo1y6VVvH/5jq0grfzFkbRUsGUnihuZRuuALCXSMpYJM/jga60xnIkWSNzxHtt6kCgYEA+CyiYQHOESIePqzWMtuuwDXaoLRT3rpzl1CU+gbFiCttQUEhQgov7RX6HiKJEt8thh3cosH9Ocy06Q1v5hrPBafgABgF+jrcx89+8McE38/bWwwrCdGKYC/p7yPb/NGR6w21VuOjSdNpvvgRLSfvVHc43Mi1E0FDppxobzDSV38CgYEA4SzLRaeoLlfck5nmGlszxPV2e2k7a/65qVhmL6+v6ALsxvIfbtC1OFVmqauRLQ5zwxsCzEkw0ZHukO5VITI1B/NHRCgFdn85xp7OwNPVDejBbvtIP/cjmgrq9UXY5teHrgy7fCxNO+WgFFUNQBoZHizGfaoXLFYSFqo0qGBaa6MCgYEAk4KkOZjDDTgEViiRYMVnB2R7QM4uk9KrZiNFiZ+AxcFaPVEjHYfEmN1ZP5QzeX4iJveU70Dy7OoaFfQgsNr6dlgkMqoU03ktRa61uxPwd7+qmGGRK1LbNvUDzVq5JXrDir0pLi48HmOFFTeJrhdwj/oVWFYMGxfNyDhrcrvIKq0CgYBGIQhR8kxr2otlIOpMTRlLf1NeTkDmepfiSRjSQzpa3QZXU7M8dR3rpJVrxHZbfQPKwQ/XBfiBXJzbYI783sErb7ZDYN+Kr0cUOAOWhKL3SQVthi9TVKou4LWanB1Yj8m/CFIf80A+pl571p1g2H/4KOPM4XuEIG5TxsDNq+ydVwKBgQD0tFpcm9NkKSDlPrg6FsgQUyxfL5MMfRFII1rhx049XUz31VRKviFqSafLQQ9xX7eJZ4uIR2TrRdTvb4K5+cfxjnTqNqEwZch+XI68aofAnl08GQzgZDXfUmGa7WE5RT+P6JRgskU6v/s24Q6E+NgyVbaZpWIVds3bsaJlBYGQ/Q==";

        //支付宝的公钥，查看地址：https://b.alipay.com/order/pidAndKey.htm 
        public static string alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgG//08NOIhEmPFeQzOkO8Tzzrkvq9ex03GfHpoyYen2X84yzhbHYnB+OQc0BtQ+oyqbHB5a8oIJHwqC7HW9LUTlnetYvCenuCePq14EftEM0UmmkaQjrjVkY6S7b/Pn94eSI0T81TET6U+JzNYlnN2aW0rcgmFVkCfh7M0oGQR6696tgRo2TClg00fpD4kWRQY4CABxOFsiJ7Fsmn1uJ+Bn5W2EDkYPnZN/Pb+JZ91Cdx8AcoQ90jYuKhQtscEEe/+YtUz73yGQkUToL5PFmoHvPUrvuAidK5K7uWfN202XA4omzPUOYA3D1k8Igty0o4pLzDbu93vCXiRGLj3fwkQIDAQAB";

        // 服务器异步通知页面路径，需http://格式的完整路径，不能加?id=123这类自定义参数,必须外网可以正常访问
        public static string notify_url = "https://www.rdkhyc.com/AliPay/NotifyHandle";

        // 页面跳转同步通知页面路径，需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
        public static string return_url = "https://www.rdkhyc.com/AliPay/ReturnHandle";

        // 签名方式
        public static string sign_type = "RSA2";

        // 调试用，创建TXT日志文件夹路径，见AlipayCore.cs类中的LogResult(string sWord)打印方法。
        public static string log_path = HttpRuntime.AppDomainAppPath.ToString() + "log/";

        // 字符编码格式 目前支持utf-8
        public static string input_charset = "utf-8";

        // 支付类型 ，无需修改
        public static string payment_type = "1.0";

        // 调用的接口名，无需修改
        public static string service = "alipay.trade.wap.pay";

        // 调用的接口名，---转账到个人账户
        public static string serviceTransfer = "alipay.fund.trans.toaccount.transfer";

        //↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

        /// <summary>
        /// 支付宝手机端开始支付
        /// </summary>
        /// <param name="body">商品描述，可空</param>
        /// <param name="subject">订单名称，必填</param>
        /// <param name="out_trade_no">商户订单号，商户网站订单系统中唯一订单号，必填</param>
        /// <param name="total_fee">付款金额，必填</param>
        public static string AlipayWapPay(string bodystr, string strOut_trade_no, decimal total_feeStr)
        {
            string body = bodystr;
            string subject = "文字描述" + "_" + bodystr;

            string out_trade_no = strOut_trade_no;
            decimal total_fee = total_feeStr;



            //开始调用支付宝
            IAopClient client = new DefaultAopClient(alipayWang, app_id, private_key, "json", "1.0", "RSA2", alipay_public_key, input_charset, false);
            ////实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称如：alipay.open.public.template.message.industry.modify 
            AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
            ////SDK已经封装掉了公共参数，这里只需要传入业务参数
            ////此次只是参数展示，未进行字符串转义，实际情况下请转义
            request.BizContent = "{" +
            "    \"body\":\"" + body + "\"," +
            "    \"subject\":\"" + subject + "\"," +
            "    \"out_trade_no\":\"" + out_trade_no + "\"," +
            "    \"total_amount\":" + total_fee + "," +
            "    \"product_code\":\"QUICK_WAP_PAY\"" +
            "  }";
            request.SetReturnUrl(return_url);
            request.SetNotifyUrl(notify_url);

            AlipayTradeWapPayResponse resp = client.pageExecute(request);
            string form = resp.Body;

            Log.Info("AlipayWapPay", "【支付宝】开始发起支付宝支付：" + request.BizContent);

            return form;
        }

        /// <summary>
        /// 支付宝转账到个人
        /// </summary>
        /// <param name="orderNo">唯一订单号</param>
        /// <param name="payee_account">收款Alipay账号</param>
        /// <param name="amount">转账金额</param>
        /// <param name="payer_show_name">付款方姓名：上海交通卡退款</param>
        /// <param name="payee_real_name">收款方真实姓名</param>
        /// <param name="remark">备注</param>
        /// <returns></returns>
        public static string AlipayTransfer(string orderNo,string payee_account,string amount,string payer_show_name,string payee_real_name,string remark)
        {
            IAopClient client = new DefaultAopClient(alipayWang, app_id, private_key, "json", "1.0", "RSA2", alipay_public_key, input_charset, false);
            AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
            request.BizContent = "{" +
            "    \"out_biz_no\":\""+ orderNo + "\"," +
            "    \"payee_type\":\"ALIPAY_LOGONID\"," +
            "    \"payee_account\":\""+ payee_account + "\"," +
            "    \"amount\":\""+ amount + "\"," +
            "    \"payer_show_name\":\""+ payer_show_name + "\"," +
            "    \"payee_real_name\":\""+ payee_real_name + "\"," +
            "    \"remark\":\""+ remark + "\"," +
            "  }";
            AlipayFundTransToaccountTransferResponse response = client.Execute(request);
            return response.Body;
            //if (response.Code == "10000" && response.Msg == "Success")//转账成功
            //{
            //    return response.Body;
            //}
            //else
            //{
            //    return response.ToString();//"调用成功:转账成功";
            //}
        }


        ///// <summary>
        ///// 查询支付宝订单支付状态
        ///// </summary>
        ///// <param name="model"></param>
        ///// <returns>true:已成功支付</returns>
        //public static bool AlipayWapPayQuery(Order model,ref string outp_no,ref DateTime? ptime)
        //{
        //    string body = model.Brief;
        //    string subject = "众享生活" + "_" + StaticData.CurrentUser.userPhone + "_" + model.OrderName;

        //    string out_trade_no = model.OrderName;
        //    decimal total_fee = model.Money;



        //    //开始调用支付宝
        //    IAopClient client = new DefaultAopClient(alipayWang, app_id, private_key, "json", "1.0", "RSA2", alipay_public_key, input_charset, false);
        //    ////实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称如：alipay.open.public.template.message.industry.modify 
        //    AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        //    ////SDK已经封装掉了公共参数，这里只需要传入业务参数
        //    ////此次只是参数展示，未进行字符串转义，实际情况下请转义
        //    request.BizContent = "{" +
        //    "    \"out_trade_no\":\"" + out_trade_no + "\"" +
        //    "  }";
        //    request.SetReturnUrl(return_url);
        //    request.SetNotifyUrl(notify_url);
        //    AlipayTradeQueryResponse resp = client.SdkExecute(request);
        //    string form = resp.Body;

        //    string str = AliPayConfigWap.alipayWang + "?" + form;
        //    string aa = WD.Framework.Utility.WDHTTPClient.HttpPost(str, "");

        //    Dictionary<string, object> ss = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(aa);
        //    var s = ss.First(x => x.Key == "alipay_trade_query_response");
        //    Dictionary<string, string> modelPay = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(Convert.ToString(s.Value));
        //    if (modelPay["code"].ToString() == "10000" && modelPay["trade_status"].ToString() == "TRADE_SUCCESS")
        //    {
        //        outp_no = modelPay["out_trade_no"];
        //        ptime = Convert.ToDateTime(modelPay["send_pay_date"]);
        //        return true;
        //    }
        //    return false ;
        //}

    }
}